<?php
class FacebookUtility
{
    public static function getUserId()
    {
        return Yii::app()->facebook->getUser();
    }

    public static function getAccessToken()
    {
        return Yii::app()->facebook->getAccessToken();
    }

    public static function setAccessToken($token)
    {
        Yii::app()->facebook->setAccessToken($token);
    }

    public static function setExtendedAccessToken()
    {
        Yii::app()->facebook->setExtendedAccessToken();
    }
    
    public static function destroySession()
    {
        Yii::app()->facebook->destroySession();
    }
    
    public static function getUserInfo($userId)
    {
        $fql = 'SELECT
                    name,
                    first_name,
                    last_name,
                    profile_url,
                    username,
                    email,
                    sex,
                    birthday_date,
                    pic_big,
                    current_location,
                    hometown_location,
                    work,
                    education
                FROM
                    user
                WHERE
                    uid = %s';
        sprintf($fql, $userId);
        $result = Yii::app()->facebook->api(array('method' => 'fql.query', 'query' => $fql));
        return $result[0];
    }
    
    public static function getLocation($id)
    {
        $fql = 'SELECT
                    name,
                    latitude,
                    longitude
                FROM
                    place
                WHERE
                    page_id = %s';
        $fql = sprintf($fql, $id);
        $result = Yii::app()->facebook->api(array('method' => 'fql.query', 'query' => $fql));
        return $result[0];
    }
    
    public static function getAlbumProfile($userId)
    {
        $fql = 'SELECT
                    src_big
                FROM
                    photo
                WHERE
                    aid IN (SELECT aid 
                           FROM album
                           WHERE owner = %s AND
                                 name = "Profile Pictures")';
        sprintf($fql, $userId);
        $result = Yii::app()->facebook->api(array('method' => 'fql.query', 'query' => $fql));
        return $result;
    }

    public static function getFriends($userId)
    {
        $fql = 'SELECT
                    uid,
                    name,
                    pic_big
                FROM
                    user
                WHERE
                    uid IN (SELECT uid2 FROM friend WHERE uid1 = %s)';
        sprintf($fql, $userId);
        $result = Yii::app()->facebook->api(array('method' => 'fql.query', 'query' => $fql));
        return $result;
    }
}